package org.zjt.spark
import scala.collection.mutable
/**
  * Created by Administrator on 2017/5/9.
  */
object MemeryWordCount extends App{

  val var1 = scala.io.Source.fromFile("./test.txt").mkString
  var map = mutable.HashMap[String, Int]()   //（X,Y）箱单于map，tuple和map无缝切换
  for ( _ <-  var1.split(",") ){
    val var2 = var1.view.flatMap(a => a.toString.split(" ")).filter(a =>if(!map.isEmpty) !map.keySet.contains(a) else true).map(a=>(a.trim,1))
    if (!var2.isEmpty)
      map += var2.reduceLeft( (a,b) => if(a._1.equals(b._1)) ( a._1, a._2 + 1)  else a )
  }
  //println(map.mkString("\n"))

  println("""偏函数""".stripMargin)
  map.foreach{case (a,b) => print(a +":"+ b+"\n") }

  println("map转化为Stream，缓存信息到流中：懒加载")
  println(map.toStream.take(111).force.mkString("\n"))


  println("view视图懒加载：懒加载")
  println(map.view.take(4).force.mkString("\n"))


}
